package com.java.userservice.controller;

import com.alibaba.fastjson.JSONObject;
import com.java.common.model.Result;
import com.java.common.util.JwtUtil;
import com.java.common.util.RedisUtil;
import com.java.userservice.model.vo.PermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/list")
    public Result list(HttpServletRequest request) {
        String token = request.getHeader("Token");
        Integer id = (Integer) JwtUtil.getClaims(token).get("id");

        // 从redis中读取权限
        String jsonStr = RedisUtil.getString(redisTemplate, "permission_" + id);
        List<PermissionVo> list = JSONObject.parseObject(jsonStr, List.class);
        return Result.success(list);
    }

    /*@GetMapping("/list")
    public Result list() {
        String jsonStr = null;

        try {
            InputStream inputStream = this.getClass().getResourceAsStream("/json/menu.json");
            InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
            int ch = 0;
            StringBuilder sb = new StringBuilder();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            reader.close();
            jsonStr = sb.toString();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return Result.success(JSONObject.parseArray(jsonStr));
    }*/

}

